package org.jetnuke.modules.calendar;

import org.apache.log4j.Logger;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.modules.calendar.utils.CalendarUtils;

public class AddEvent implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(AddEvent.class);

	// hour=12&year=2006&month=2&day=1
	int hour = -1;
	int year;
	int month = -1;
	int day;

	public void setDay(int day) {
		this.day = day;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		CalendarUtils.putCommonObjects(vc);
		List eventTypes = DataAccessDriver.getInst().newCalendarEventTypeDAO()
				.selectAll();
		vc.put("eventTypes", eventTypes);
		GregorianCalendar today = new GregorianCalendar();
		if (year == 0) {
			year = today.get(Calendar.YEAR);
		}
		if (month == -1) {
			month = today.get(Calendar.MONTH);
		} else
			month = month - 1;
		if (day == 0) {
			day = today.get(Calendar.DATE);
		}
		if (hour == -1) {
			hour = 10;
		}
		GregorianCalendar the7Day = new GregorianCalendar(year, month, day);
		the7Day.add(Calendar.DATE, 7);
		int date7Day = the7Day.get(Calendar.DATE);
		int month7Day = the7Day.get(Calendar.MONTH);
		int year7Day = the7Day.get(Calendar.YEAR);
		vc.put("startYear", new Integer(year));
		vc.put("startMonth", new Integer(month));
		vc.put("startDay", new Integer(day));
		vc.put("startHour", new Integer(hour));
		vc.put("thesevenday", new Integer(date7Day));
		vc.put("monthsevenDay", new Integer(month7Day));
		vc.put("yearseventDay", new Integer(year7Day));
		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

}
